/** Copyright (c) 2008-2009, Markos Evlogimenos
 *
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in
 *       the documentation and/or other materials provided with the
 *       distribution.
 *     * The name of Markos Evlogimenos may be used to endorse or
 *		 promote products derived from this software without specific
 *		 prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */

package game.packs;

import java.util.BitSet;

import org.kamimark.enums._EnumInterface;

public class BoolPack<ENUMTYPE extends _EnumInterface> implements _Pack<ENUMTYPE>, Comparable<BoolPack<ENUMTYPE>>
{

	private _EnumInterface[] names;

	private BitSet data;

	public BoolPack(final _EnumInterface[] names) {
		this(names.length);
		this.names = names;
	}

	private BoolPack(final int size) {
		data = new BitSet(size);
	}

	@Override
	public BoolPack<ENUMTYPE> clone() {
		final BoolPack<ENUMTYPE> newPack = new BoolPack<ENUMTYPE>(names.length);
		newPack.names = names;
		newPack.data = (BitSet) data.clone();
		return newPack;
	}

	/**
	 * @param toCompare -
	 *            BoolPack to compare to.
	 * @return
	 *            <ul>
	 *            <li> 1 - All the flags of
	 * @param exist
	 *            in
	 * @this</li>
	 *       <li> 0 - The
	 * @param and
	 * @this have the same flags</li>
	 *       <li> -1 - As not 1</li>
	 *       </ul>
	 */
	public int compareTo(final BoolPack<ENUMTYPE> toCompare) {
		if (equals(toCompare))
			return 0;
		final BitSet test = (BitSet) data.clone();
		test.and(toCompare.data);
		if (test.equals(toCompare.data))
			return 1;
		return -1;
	}

	public boolean equals(final BoolPack<ENUMTYPE> toCompare) {
		return toCompare.data.equals(data);
	}

	public BitSet getElements() {
		return data;
	}

	public _EnumInterface[] getNames() {
		return names;
	}

	@Override
	public String toString() {
		final StringBuilder result = new StringBuilder();
		for (int i = 0; i < names.length; i++)
			if (data.get(i)) {
				result.append(names[i]);
				result.append("\n");
			}
		return result.toString();
	}

}
